package cn.hh.org.service.impl;


import cn.hh.basic.execption.BusinessException;
import cn.hh.basic.service.impl.BasicServiceImpl;
import cn.hh.basic.util.MD5Utils;
import cn.hh.org.domain.Employee;
import cn.hh.org.domain.Shop;
import cn.hh.org.mapper.EmpMapper;
import cn.hh.org.mapper.ShopMapper;
import cn.hh.org.service.IShopService;
import cn.hh.user.domain.LoginInfo;
import cn.hh.user.mapper.LoginInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
public class ShopServiceImpl extends BasicServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    @Transactional
    public void add(Shop shop) {
        /*1注册校验*/
        /*1.1空校验：前端校验过了，后台可以省略*/
        /*1.2用户是否被注册，直接登录*/
        Employee employee = shop.getEmployee();
        /*查询注册信息*/
        Employee employeeObj = empMapper.loadByUser(employee);
        /*如果有注册过*/
        if(employeeObj != null){
            throw new BusinessException("店铺已经注册，请直接登录，如果忘记密码，请找回密码!!");
        }
        /*给密码加盐*/
        //生成随机的盐
        UUID salt = UUID.randomUUID();
        // 将对象转为字符串
        employee.setSalt(salt.toString());
        //将密码加盐加密过后进行存储
        employee.setPassword(MD5Utils.encrypByMd5(employee.getPassword()+salt));
        /*2入驻*/
        /*2.1先保存管理员信息*/
        empMapper.add(employee);
        /*2.2保存shop*/
        shop.setEmployee(employee);
        /*设置状态为1，表明直接通过，不需要审核*/
        shop.setState(0);
        /*保存shop*/
        shopMapper.add(shop);
        /*修改员工信息绑定Shop*/
        employee.setShop_id(shop.getId());
        /*绑定登录数据库*/
        LoginInfo logininfo = new LoginInfo();
        logininfo.setUsername(employee.getUsername());
        logininfo.setPhone(employee.getPhone());
        logininfo.setEmail(employee.getEmail());
        logininfo.setSalt(employee.getSalt());
        logininfo.setPassword(employee.getPassword());
        logininfo.setType(0);
        logininfo.setDisable(1);
        loginInfoMapper.add(logininfo);
        System.out.println("添加返回的主键"+logininfo.getId());
        employee.setLogininfo_id(logininfo.getId());
        empMapper.update(employee);
        /*3短信邮件通知注册成功（默认通过审核）*/
        //消息的核心对象
        SimpleMailMessage message = new SimpleMailMessage();
        //设置发件人
        message.setFrom("179235091@qq.com");
        //设置收件人
        message.setTo(employee.getEmail());
        //设置主题
        message.setSubject("注册审核中");
        //设置正文
        message.setText("您的账户注册审核中，审核结果将通过此邮件发送给您！！！");
        //发送邮件
        javaMailSender.send(message);
    }
    /*审核时不允许选择管理员*/
    @Override
    public void update(Shop shop) {
        /*获取id*/
        Long id = shop.getId();
        /*通过id获取之前数据*/
        Shop shopById = shopMapper.loadById(id);
        /*设置管理员id*/
        shop.setAdmin_id(shopById.getAdmin_id());
        shopMapper.update(shop);
        /*审核结果通知*/
        /*获取收件人信息*/
        Employee employee = empMapper.loadById(shop.getAdmin_id());
        /*获取审核状态*/
        Integer state = shop.getState();
        if(state==1){
            /*注册成功表明信息正确，保存至登录信息*/
            /*将信息同步至登陆表*/
            LoginInfo logininfo = new LoginInfo();
            logininfo.setUsername(shop.getName());
            logininfo.setPhone(employee.getPhone());
            logininfo.setEmail(employee.getEmail());
            logininfo.setSalt(employee.getSalt());
            logininfo.setPassword(employee.getPassword());
            logininfo.setType(0);
            logininfo.setDisable(1);
            loginInfoMapper.add(logininfo);
            System.out.println("+++++++"+logininfo.toString());
            /*设置user表关联logininfo*/
            employee.setLogininfo_id(logininfo.getId());
            empMapper.update(employee);
            /*3短信邮件通知注册成功（默认通过审核）*/
            //消息的核心对象
            SimpleMailMessage message = new SimpleMailMessage();
            //设置发件人
            message.setFrom("179235091@qq.com");
            //设置收件人
            message.setTo(employee.getEmail());
            //设置主题
            message.setSubject("注册审核结果");
            //设置正文
            message.setText("恭喜您，账户已审核通过，请尽情快乐起来吧！！！");
            //发送邮件
            javaMailSender.send(message);
        }else if(state==-1){
            /*3短信邮件通知注册成功（默认通过审核）*/
            //消息的核心对象
            SimpleMailMessage message = new SimpleMailMessage();
            //设置发件人
            message.setFrom("179235091@qq.com");
            //设置收件人
            message.setTo(employee.getEmail());
            //设置主题
            message.setSubject("注册审核结果");
            //设置正文
            message.setText("很遗憾，您的账户信息需要进一步完善，稍后将有客服人员联系您进行核对校验！！！");
            //发送邮件
            javaMailSender.send(message);
        }
    }

}
